package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICompanyApproSettingDAO;
import org.jsola.hr.dto.CompanyApproSettingAddDTO;
import org.jsola.hr.dto.CompanyApproSettingUpdateDTO;
import org.jsola.hr.entity.CompanyApproSettingDO;
import org.jsola.hr.query.CompanyApproSettingQuery;
import org.jsola.hr.service.ICompanyApproSettingService;
import org.jsola.hr.vo.CompanyApproSettingListVO;
import org.jsola.hr.vo.CompanyApproSettingVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrCompanyApproSettingServiceImpl")
public class CompanyApproSettingServiceImpl implements ICompanyApproSettingService {

    @Autowired
    private ICompanyApproSettingDAO companyApproSettingDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CompanyApproSettingVO save(CompanyApproSettingAddDTO companyApproSettingAddDTO, TokenUser tokenUser) {
        // 转DO
        CompanyApproSettingDO companyApproSettingDO = companyApproSettingAddDTO.to(CompanyApproSettingDO.class);
        // 保存
        companyApproSettingDO = save(companyApproSettingDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return companyApproSettingDO.to(CompanyApproSettingVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CompanyApproSettingUpdateDTO companyApproSettingUpdateDTO, TokenUser tokenUser) {
        //转DO
        CompanyApproSettingDO companyApproSettingDO = companyApproSettingUpdateDTO.to(CompanyApproSettingDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(companyApproSettingDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... companyApproSettingIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) companyApproSettingIds);
    }


    @Override
    public CompanyApproSettingVO selectById(String companyApproSettingId, String siteId) {
        CompanyApproSettingDO companyApproSettingDO = selectDOById(companyApproSettingId, siteId);
        if (companyApproSettingDO == null) {
            return null;
        }
        return companyApproSettingDO.to(CompanyApproSettingVO.class);
    }

    @Override
    public List<CompanyApproSettingListVO> select(CompanyApproSettingQuery companyApproSettingQuery, String siteId) {
        List<CompanyApproSettingDO> companyApproSettingDOList = selectDO(companyApproSettingQuery, siteId);
        if(CollectionUtils.isEmpty(companyApproSettingDOList)) {
            return companyApproSettingDOList == null ? null : new ArrayList<>();
        }
        return companyApproSettingDOList.stream()
                .map(companyApproSettingDO -> companyApproSettingDO.to(CompanyApproSettingListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CompanyApproSettingQuery companyApproSettingQuery, String siteId) {
        Example example = buildExample(companyApproSettingQuery,siteId);
        return companyApproSettingDAO.selectCountByExample(example);
    }

    @Override
    public Page<CompanyApproSettingListVO> selectPage(CompanyApproSettingQuery companyApproSettingQuery, String siteId) {
        Example example = buildExample(companyApproSettingQuery,siteId);
        Page<CompanyApproSettingDO> page = companyApproSettingDAO.selectPageByExample(example,
                companyApproSettingQuery.getPageNo(),
                companyApproSettingQuery.getPageSize());

        return page.to(CompanyApproSettingListVO.class);
    }

    @Override
    public CompanyApproSettingDO selectDOById(String companyApproSettingId, String siteId) {
        return listById(companyApproSettingId, siteId);
    }

    @Override
    public List<CompanyApproSettingDO> selectDO(CompanyApproSettingQuery companyApproSettingQuery, String siteId) {
        Example example = buildExample(companyApproSettingQuery,siteId);
        return companyApproSettingDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param companyApproSettingQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CompanyApproSettingQuery companyApproSettingQuery, String siteId) {
        Example example = new Example(CompanyApproSettingDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (companyApproSettingQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", companyApproSettingQuery.getCompanyId());
        }
        if (companyApproSettingQuery.getApproFlowId() != null) {
            example.and().andEqualTo("approFlowId", companyApproSettingQuery.getApproFlowId());
        }
        if (companyApproSettingQuery.getIsOpen() != null) {
            example.and().andEqualTo("isOpen", companyApproSettingQuery.getIsOpen());
        }
        // 排序
        ExampleKit.setExampleOrder(example,companyApproSettingQuery.getOrders());
        return example;
    }
}




